Page({
    data: {
      // 假设初始已有两条地址，实际项目记得从接口加载
      addresses: [
        {
          id: 1,
          name: '张三',
          phone: '13812341234',
          province: '北京市',
          city: '北京市',
          area: '朝阳区',
          detail: '望京SOHO T2',
          isDefault: true
        },
        {
          id: 2,
          name: '李四',
          phone: '15823452345',
          province: '广东省',
          city: '广州市',
          area: '天河区',
          detail: '华南理工大学',
          isDefault: false
        }
      ],
      showEdit: false,           // 控制弹窗
      editAddress: {},           // 当前编辑的地址
      editMode: 'add'            // add / edit
    },
    chooseAddress(e) {
        const address = e.currentTarget.dataset.address;
        wx.setStorageSync('selectedAddress', address);
        wx.navigateBack();
      },
  
    // 新增地址
    onAdd() {
      this.setData({
        showEdit: true,
        editMode: 'add',
        editAddress: {
          id: '',
          name: '',
          phone: '',
          province: '',
          city: '',
          area: '',
          detail: '',
          isDefault: false
        }
      });
    },
  
    // 编辑
    onEdit(e) {
      const id = e.currentTarget.dataset.id;
      const address = this.data.addresses.find(item => item.id === id);
      this.setData({
        showEdit: true,
        editMode: 'edit',
        editAddress: Object.assign({}, address)
      });
    },
  
    // 删除
    onDelete(e) {
      const id = e.currentTarget.dataset.id;
      wx.showModal({
        title: '提示',
        content: '确认删除该地址吗？',
        success: res => {
          if(res.confirm) {
            this.setData({
              addresses: this.data.addresses.filter(item => item.id !== id)
            });
          }
        }
      });
    },
  
    // 取消编辑
    onCancelEdit() {
      this.setData({ showEdit: false });
    },
  
    // 保存编辑/新增
    onSaveEdit() {
      const { editAddress, addresses, editMode } = this.data;
      // 简单的校验
      if(!editAddress.name || !editAddress.phone || !editAddress.province || 
        !editAddress.city || !editAddress.area || !editAddress.detail) {
        wx.showToast({ title: '请填写完整', icon: 'none' });
        return;
      }
      if(editMode === 'add') {
        // 处理ID自增
        editAddress.id = Date.now();
        let newAddresses = addresses.concat();
        // 若设为默认，取消其他默认
        if(editAddress.isDefault) {
          newAddresses = newAddresses.map(item => ({...item, isDefault: false}))
        }
        newAddresses.push(editAddress);
        this.setData({
          addresses: newAddresses,
          showEdit: false
        });
      } else {
        // 编辑更新
        let newAddresses = addresses.map(item => {
          if(item.id === editAddress.id) {
            return editAddress;
          }
          // 当前如果本条设为默认，其余需取消默认
          if(editAddress.isDefault) {
            item.isDefault = false;
          }
          return item;
        })
        this.setData({
          addresses: newAddresses,
          showEdit: false
        });
      }
    },
  
    // 编辑表单输入（单独写一组，不影响核心逻辑。可用一个通用方法优化）
    onInputName(e) {
      this.setData({ 'editAddress.name': e.detail.value });
    },
    onInputPhone(e) {
      this.setData({ 'editAddress.phone': e.detail.value });
    },
    onInputProvince(e) {
      this.setData({ 'editAddress.province': e.detail.value });
    },
    onInputCity(e) {
      this.setData({ 'editAddress.city': e.detail.value });
    },
    onInputArea(e) {
      this.setData({ 'editAddress.area': e.detail.value });
    },
    onInputDetail(e) {
      this.setData({ 'editAddress.detail': e.detail.value });
    },
    onChangeDefault(e) {
      this.setData({ 'editAddress.isDefault': e.detail.value.length > 0 });
    }
  })